/*----------------------------------
Analysis code for:
A Registered Report on Place-Based Resentment: Exploring Urban-Rural Tensions in Sweden
------------------------------------*/

clear all // Reset memory and datasets

// Set working directory
cd "/Users/ka4012ha/Documents/Research/Papers/Submitted papers/Rural and Urban (CA JEPS)/data/Regressions"

// Import dta dataset (first row has variable names)
use "/Users/ka4012ha/Documents/Research/Papers/Submitted papers/Rural and Urban (CA JEPS)/For OSF/4. Dataverse/2.MainData_RuralResent.dta"

// -------------------------------------------------------------
// Install required packages and set graph scheme
// -------------------------------------------------------------
ssc install estout, replace          // Output tables
ssc install fsum                     // Summary stats
ssc install betterbar                 // Improved bar plots
net install schemepack, from("https://raw.githubusercontent.com/asjadnaqvi/stata-schemepack/main/installation/") replace
set scheme cblind1                    // Colorblind-friendly scheme
net install grc1leg, from("http://www.stata.com/users/vwiggins/")
ssc install outreg2                   // Regression output formatting

// -------------------------------------------------------------
// 1. RESULTS IN MAIN TEXT
//-------------------------------------------------------------

// -------------------------------------------------------------
// 1.1 DESCRIPTIVE RESULTS (Section 4.1)
// -------------------------------------------------------------

// H1: Place-based identity strength
ttest ID_strength, by(Rural_ID_Bin)           // Mean difference test
esize twosample ID_strength, by(Rural_ID_Bin) // Effect size

// H2: Place-based resentment score
ttest resent, by(Rural_ID_Bin)
esize twosample resent, by(Rural_ID_Bin)

// H3: Place-based stereotyping (PTEI)
ttest PTEI, by(Rural_ID_Bin)
esize twosample PTEI, by(Rural_ID_Bin)

// Table B1 in Appendix: Regression models for H1–H3
regress ID_strength Rural_ID_Bin male Age Uni Pol_Right, robust
outreg2 using RegressionB1.doc, replace label ctitle("Identity strength") bdec(3)
regress resent Rural_ID_Bin male Age Uni Pol_Right, robust
outreg2 using RegressionB1.doc, append label ctitle("Place resentment") bdec(3)
regress PTEI Rural_ID_Bin male Age Uni Pol_Right, robust
outreg2 using RegressionB1.doc, append label ctitle("Trait Evaluation Index") bdec(3)

// -------------------------------------------------------------
// 1.2 MAIN RESULTS: Causal effect of place-based cues (Section 4.2)
// -------------------------------------------------------------

// Rural respondents
ttest policy if Rural_ID_Bin==2, by(ruralPolitician)
esize twosample policy if Rural_ID_Bin==2, by(ruralPolitician)

// Urban respondents
ttest policy if Rural_ID_Bin==1, by(ruralPolitician)
esize twosample policy if Rural_ID_Bin==1, by(ruralPolitician)

// Table B2 in Appendix: Regression models for H4a (Rural) and H4b (Urban)
regress policy ruralPolitician Gender Age Uni Pol_Right if Rural_ID_Bin==2, robust
outreg2 using RegressionB2.doc, replace label ctitle("Rural respondents") bdec(3)
regress policy ruralPolitician Gender Age Uni Pol_Right if Rural_ID_Bin==1, robust
outreg2 using RegressionB2.doc, append label ctitle("Urban respondents") bdec(3)

// -------------------------------------------------------------
// 2. RESULTS IN APPENDIX
// -------------------------------------------------------------

// -------------------------------------------------------------
// Section B2: Interaction effects (Exploratory analyses)
// -------------------------------------------------------------

// Table B5: In-group identity × politician cue
regress policy i.ruralPolitician##i.Rural_ID_Bin, robust
outreg2 using AppendixRegB5.doc, replace label ctitle("Level of agreement") bdec(3)
regress policy i.ruralPolitician##i.Rural_ID_Bin male Age Uni Pol_Right, robust
outreg2 using AppendixRegB5.doc, append label ctitle("Level of agreement") bdec(3)

// Table B6: Identity strength × politician cue
regress policy ruralPolitician ID_strength Gender Age Uni Pol_Right if Rural_ID_Bin==2, robust
outreg2 using InteractIDstrength.doc, replace label ctitle("Rural: Identity strength") bdec(3)
regress policy i.ruralPolitician##c.ID_strength Gender Age Uni Pol_Right if Rural_ID_Bin==2, robust
outreg2 using InteractIDstrength.doc, append label ctitle("Rural: Identity strength") bdec(3)
regress policy ruralPolitician ID_strength Gender Age Uni Pol_Right if Rural_ID_Bin==1, robust
outreg2 using InteractIDstrength.doc, append label ctitle("Urban: Identity strength") bdec(3)
regress policy i.ruralPolitician##c.ID_strength Gender Age Uni Pol_Right if Rural_ID_Bin==1, robust
outreg2 using InteractIDstrength.doc, append label ctitle("Urban: Identity strength") bdec(3)

// Table B7: Resentment × politician cue
regress policy ruralPolitician resent Gender Age Uni Pol_Right if Rural_ID_Bin==2, robust
outreg2 using InteractResent.doc, replace label ctitle("Rural: Resentment") bdec(3)
regress policy i.ruralPolitician##c.resent Gender Age Uni Pol_Right if Rural_ID_Bin==2, robust
outreg2 using InteractResent.doc, append label ctitle("Rural: Resentment") bdec(3)
regress policy ruralPolitician resent Gender Age Uni Pol_Right if Rural_ID_Bin==1, robust
outreg2 using InteractResent.doc, append label ctitle("Urban: Resentment") bdec(3)
regress policy i.ruralPolitician##c.resent Gender Age Uni Pol_Right if Rural_ID_Bin==1, robust
outreg2 using InteractResent.doc, append label ctitle("Urban: Resentment") bdec(3)

// Table B8: PTEI × politician cue
regress policy ruralPolitician PTEI Gender Age Uni Pol_Right if Rural_ID_Bin==2, robust
outreg2 using InteractPTEI.doc, replace label ctitle("Rural: PTEI") bdec(3)
regress policy i.ruralPolitician##c.PTEI Gender Age Uni Pol_Right if Rural_ID_Bin==2, robust
outreg2 using InteractPTEI.doc, append label ctitle("Rural: PTEI") bdec(3)
regress policy ruralPolitician PTEI Gender Age Uni Pol_Right if Rural_ID_Bin==1, robust
outreg2 using InteractPTEI.doc, append label ctitle("Urban: PTEI") bdec(3)
regress policy i.ruralPolitician##c.PTEI Gender Age Uni Pol_Right if Rural_ID_Bin==1, robust
outreg2 using InteractPTEI.doc, append label ctitle("Urban: PTEI") bdec(3)

// -------------------------------------------------------------
// Section B3: Each policy proposal separately
// -------------------------------------------------------------

// T-tests by Rural/Urban group
ttest EducPol if Rural_ID_Bin==2, by(ruralPolitician)
ttest EducPol if Rural_ID_Bin==1, by(ruralPolitician)
ttest HealthPol if Rural_ID_Bin==2, by(ruralPolitician)
ttest HealthPol if Rural_ID_Bin==1, by(ruralPolitician)
ttest ImmigPol if Rural_ID_Bin==2, by(ruralPolitician)
ttest ImmigPol if Rural_ID_Bin==1, by(ruralPolitician)

// Table B3: Regression by group and policy area
regress EducPol ruralPolitician male Age Uni Pol_Right if Rural_ID_Bin==1, robust
outreg2 using EachProposalURBAN.doc, replace label ctitle("Education: Level of agreement") bdec(3)
regress HealthPol ruralPolitician male Age Uni Pol_Right if Rural_ID_Bin==1, robust
outreg2 using EachProposalURBAN.doc, append label ctitle("Health: Level of agreement") bdec(3)
regress ImmigPol ruralPolitician male Age Uni Pol_Right if Rural_ID_Bin==1, robust
outreg2 using EachProposalURBAN.doc, append label ctitle("Immigration: Level of agreement") bdec(3)

regress EducPol ruralPolitician male Age Uni Pol_Right if Rural_ID_Bin==2, robust
outreg2 using EachProposalRURAL.doc, replace label ctitle("Education: Level of agreement") bdec(3)
regress HealthPol ruralPolitician male Age Uni Pol_Right if Rural_ID_Bin==2, robust
outreg2 using EachProposalRURAL.doc, append label ctitle("Health: Level of agreement") bdec(3)
regress ImmigPol ruralPolitician male Age Uni Pol_Right if Rural_ID_Bin==2, robust
outreg2 using EachProposalRURAL.doc, append label ctitle("Immigration: Level of agreement") bdec(3)

// -------------------------------------------------------------
// Section B4: Overall effect of rural politician cue
// -------------------------------------------------------------

// Pooled sample T-tests and effect sizes
ttest policy, by(ruralPolitician)
esize twosample policy, by(ruralPolitician)
ttest policy if AttentionA1==6, by(ruralPolitician) // Robustness: exclude failed attention

ttest policy, by(ruralPolitician)
esize twosample policy, by(ruralPolitician)
ttest HealthPol, by(ruralPolitician)
esize twosample HealthPol, by(ruralPolitician)
ttest EducPol, by(ruralPolitician)
esize twosample EducPol, by(ruralPolitician)
ttest ImmigPol, by(ruralPolitician)
esize twosample ImmigPol, by(ruralPolitician)

// Table B9: Pooled regressions by policy area
regress HealthPol ruralPolitician Rural_ID_Bin male Age Uni Pol_Right, robust
outreg2 using Explore1.doc, replace label ctitle("Health") bdec(3)
regress EducPol ruralPolitician Rural_ID_Bin male Age Uni Pol_Right, robust
outreg2 using Explore1.doc, append label ctitle("Education") bdec(3)
regress ImmigPol ruralPolitician Rural_ID_Bin male Age Uni Pol_Right, robust
outreg2 using Explore1.doc, append label ctitle("Immigration") bdec(3)
regress policy ruralPolitician Rural_ID_Bin male Age Uni Pol_Right, robust
outreg2 using Explore1.doc, append label ctitle("General policy agreement") bdec(3)

// -------------------------------------------------------------
// ROBUSTNESS CHECKS: Test if results are robust also when excluding those who failed the attention check
// -------------------------------------------------------------

count if AttentionA1!=6 // Count respondents failing attention check

// 1. Robustness for H1–H3
ttest ID_strength if AttentionA1==6, by(Rural_ID_Bin)
esize twosample ID_strength if AttentionA1==6, by(Rural_ID_Bin)
ttest resent if AttentionA1==6, by(Rural_ID_Bin)
esize twosample resent if AttentionA1==6, by(Rural_ID_Bin)
ttest PTEI if AttentionA1==6, by(Rural_ID_Bin)
esize twosample PTEI if AttentionA1==6, by(Rural_ID_Bin)

// 2. Robustness regressions for H1–H3
regress ID_strength Rural_ID_Bin male Age Uni Pol_Right if AttentionA1==6, robust
outreg2 using Robust1.doc, replace label ctitle("Identity strength") bdec(3)
regress resent Rural_ID_Bin male Age Uni Pol_Right if AttentionA1==6, robust
outreg2 using Robust1.doc, append label ctitle("Place resentment") bdec(3)
regress PTEI Rural_ID_Bin male Age Uni Pol_Right if AttentionA1==6, robust
outreg2 using Robust1.doc, append label ctitle("Trait Evaluation Index") bdec(3)

// 3. Robustness for H4
// H4a: Rural respondents
ttest policy if Rural_ID_Bin==2 & AttentionA1==6, by(ruralPolitician)
esize twosample policy if AttentionA1==6 & Rural_ID_Bin==2, by(ruralPolitician)

// H4b: Urban respondents
ttest policy if Rural_ID_Bin==1 & AttentionA1==6, by(ruralPolitician)
esize twosample policy if AttentionA1==6 & Rural_ID_Bin==1, by(ruralPolitician)

// 4. Robustness regressions for H4
regress policy ruralPolitician Gender Age Uni Pol_Right if Rural_ID_Bin==2 & AttentionA1==6, robust
outreg2 using Robust2.doc, replace label ctitle("Rural respondents") bdec(3)
regress policy ruralPolitician Gender Age Uni Pol_Right if Rural_ID_Bin==1 & AttentionA1==6, robust
outreg2 using Robust2.doc, append label ctitle("Urban respondents") bdec(3)
